﻿using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace SocketLib
{

    public class RegexHelper
    {
        #region 验证输入字符串是否与模式字符串匹配  
        /// <summary>  
        /// 验证输入字符串是否与模式字符串匹配，匹配返回true  
        /// </summary>  
        /// <param name="input">输入字符串</param>  
        /// <param name="pattern">模式字符串</param>          
        public static bool IsMatch(string input, string pattern)
        {
            return IsMatch(input, pattern, RegexOptions.IgnoreCase);
        }

        /// <summary>  
        /// 验证输入字符串是否与模式字符串匹配，匹配返回true  
        /// </summary>  
        /// <param name="input">输入的字符串</param>  
        /// <param name="pattern">模式字符串</param>  
        /// <param name="options">筛选条件</param>  
        public static bool IsMatch(string input, string pattern, RegexOptions options)
        {
            return Regex.IsMatch(input, pattern, options);
        }
        #endregion
        public static string getAttr(string str, string attr)
        {
            
            string pa = ".*?[\\s|\\(]" + attr + "\\s?=\\s?\"(.*?)\"";
            Regex reg = new Regex(pa, RegexOptions.IgnoreCase);
            
            GroupCollection colls = Regex.Match(str, pa, RegexOptions.IgnoreCase).Groups;
            if (colls.Count > 1)
            {
                return colls[1].Value.ToString();
            }
            else
            {
                return "";
            }
        }
        public static ArrayList getAttrs(string str, string attr)
        {
            ArrayList result = new ArrayList();
            string pa = ".*?\\s" + attr + "=\"(.*?)\"";
            Regex reg = new Regex(pa, RegexOptions.IgnoreCase);
            MatchCollection mc = reg.Matches(str);
            for (int i = 0; i < mc.Count; i++)
            {
                if (mc[i].Groups.Count > 1)
                {
                    result.Add(mc[i].Groups[1].Value.ToString());
                }
                else
                {
                    result.Add("");
                }
            }
            return result;
        }


    }
}
